package com.dianping.tpfun.supervised.decisiontree.utils;

/**
 * Created by ztw on 16/1/10.
 */
public class CalculateUtils {

    //返回升序数组中大于等于target的第一个数字下标（多个相等数字返回最左侧的）
    public static int getFirstLessThanParamIndex(Comparable targetNum,Comparable[] nums){
        int high=nums.length-1;
        int low=0;
        int middle=(high+low)/2;
        int resultIndex=-1;
        if(null==nums){
            return -1;
        }
        while(high>=low){
            middle=(high+low)/2;
            if(nums[middle].compareTo(targetNum)<0){
                low=middle+1;
            }
            else {
                high=middle-1;
                resultIndex=middle;
            }
        }
        if(low>=nums.length){
            //说明所有数都小于target
            return -1;
        }
        //左移
        while(resultIndex>0&&nums[resultIndex]==nums[resultIndex-1]){
            resultIndex--;
        }
        return resultIndex;
    }

}
